#pragma once
#include<vector>



template<class K>
class HashFunc
{
public:
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};

template<>
class HashFunc<string>
{
public:
	size_t operator()(const string& key)
	{
		size_t hash = 0;
		for (auto& x : key)
		{
			hash += x;
			hash *= 131;
		}

		return hash;
	}

};

namespace hash_bucket
{
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;
		HashNode(const T& data)
			:_data(data), _next(nullptr)
		{

		}
		bool operator==(const HashNode& hs)
		{
			return _data == hs._data;
		}

		bool operator!=(const HashNode& hs)
		{
			return _data != hs._data;
		}
	};


	template<class K, class T, class Hash, class keyofT>
	class HashTable;

	template<class K,class T,class Ref,class Ptr,class Hash,class KeyofT>
	struct __HTIterator
	{
		typedef HashNode<T> Node;
		typedef HashTable<K, T, Hash, KeyofT> HT;
		typedef __HTIterator<K, T, Ref, Ptr, Hash, KeyofT> Self;


		Node* _node;
		const HT* _ht;


		__HTIterator(Node* node, const HT* ht)
			:_node(node), _ht(ht)
		{

		}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}


		Self operator++()
		{
			if (_node->_next)
			{
				_node = _node->_next;
			}
			else
			{
				Hash hs;
				KeyofT kt;
				size_t hashi = hs(kt(_node->_data)) % _ht->_tables.size();
				++hashi;
				while (hashi < _ht->_tables.size())
				{
					if (_ht->_tables[hashi])
					{
						_node = _ht->_tables[hashi];
						break;
					}
					else
					{
						++hashi;
					}
				}
				if (hashi == _ht->_tables.size())
				{
					_node= nullptr;
				}
			}

			return *this;
		}


		bool operator==(const Self& s)
		{
			return _node == s._node;
		}

		bool operator!=(const Self& s)
		{
			return _node != s._node;
		}


	};



	template<class K, class T ,class Hash,class keyofT>
	class HashTable
	{
		template<class K, class T, class Ref, class Ptr, class Hash, class KeyofT>
		friend struct __HTIterator;

		typedef HashNode<T> Node;
		
	public:
		typedef __HTIterator<K, T, T&,  T* ,Hash, keyofT> Iterator;
		typedef __HTIterator<K, T, const T&, const T* ,Hash, keyofT> Const_Iterator;

		Iterator Begin()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
				{
					return Iterator(cur,this);
				}

			}

			return Iterator(nullptr, this);
			
		}


		Iterator End()
		{
			return Iterator(nullptr, this);
		}


		Const_Iterator Begin()const
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
				{
					return Const_Iterator(cur, this);
				}

			}

			return Const_Iterator(nullptr, this);

		}


		Const_Iterator End()const
		{
			return Const_Iterator(nullptr, this);
		}
		

	private:
		inline unsigned long __stl_next_prime(unsigned long n)
		{
			// Note: assumes long is at least 32 bits.
			static const int __stl_num_primes = 28;
			static const unsigned long __stl_prime_list[__stl_num_primes] =
			{
				53, 97, 193, 389, 769,
				1543, 3079, 6151, 12289, 24593,
				49157, 98317, 196613, 393241, 786433,
				1572869, 3145739, 6291469, 12582917, 25165843,
				50331653, 100663319, 201326611, 402653189, 805306457,
				1610612741, 3221225473, 4294967291
			};
			const unsigned long* first = __stl_prime_list;
			const unsigned long* last = __stl_prime_list + __stl_num_primes;
			const unsigned long* pos = lower_bound(first, last, n);
			return pos == last ? *(last - 1) : *pos;
		}

	public:

		HashTable()
		{
			_tables.resize(__stl_next_prime(1));
		}

		pair<Iterator,bool> Insert(const T& data)
		{
			keyofT kt;
			Hash hs;
			Iterator find=Find(kt(data));
			if (find != End())
			{
				return { find,false };
			}

			if (_n == _tables.size())
			{
				size_t newsize = __stl_next_prime(_tables.size() + 1);
				vector<Node*> newtables(newsize);

				for (size_t i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						size_t hashi = hs(kt(cur->_data)) % newsize;
						cur->_next = newtables[hashi];
						newtables[hashi] = cur;
						cur = next;

					}
					_tables[i] = nullptr;

				}
				_tables.swap(newtables);
			}

			size_t hashi = hs(kt(data))% _tables.size();
			Node* newnode = new Node(data);
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			++_n;
			return { {newnode,this},true };

		}

		Iterator Find(const K& key)
		{
			keyofT kt;
			Hash hs;
			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kt(cur->_data)== key)
				{
					return Iterator(cur,this);
				}
				cur = cur->_next;
			}
			return Iterator(nullptr,this);
		}

		bool Erase(const K& key)
		{
			keyofT kt;
			Hash hs;
			size_t hashi = hs(kt(key)) % _tables.size();
			Node* prev = nullptr;
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (cur->_data == key)
				{
					if (prev == nullptr)
					{
						_tables[hashi] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}
					delete cur;
					return true;
				}
				prev = cur;
				cur = cur->_next;

			}

			return false;
		}


	private:
		vector<Node*> _tables;
		size_t _n = 0;

	};


	/*void TestHT1()
	{
		HashTable<int, int> ht1;
		ht1.Insert({ 54, 1 });
		ht1.Insert({ 1, 1 });

		for (int i = 0; i < 53; i++)
		{
			ht1.Insert({ rand(), i });
		}

		ht1.Erase(23281);
		ht1.Erase(1);
	}*/
}



